#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "users.h"
#include "tcp.h"
#include "pack.h"

FILE *open_db()				//打开文件
{
	FILE *fp = NULL;
	fp = fopen("./bin/data/userdata.txt", "rt+");
	if(fp == NULL)
		printf("date base error\n");
	return fp;
}

int regist(char *Account, char *Password, char *Nickname)		//注册
{
	FILE *fp = NULL;
	fp = open_db();
	char blank[50];
	memset(blank, 0, sizeof(blank));
	if(occupy(Account))									//判断账号是否存在
		return 1;
	fseek(fp, 0, SEEK_END);									//偏移文件尾部
	fprintf(fp, "%-20s%-20s%-20s%-50s%-50s%-3d\n", 
		Account, Password, Nickname, "blank" , "blank", 1);
	fclose(fp);
	return 0;	//success
}

int occupy(char *Account)				//判断账号是否存在
{
	FILE *fp = NULL;
	fp = open_db();
	char date[170];

	fseek(fp, 0, SEEK_SET);			//偏移文件开头
	while(feof(fp) == 0)			//文件未读取完
	{
		memset(date, 0, sizeof(date));
		fgets(date, sizeof(date), fp);	//读一行
		if(!strncmp(date, Account, strlen(Account)))
			return 1;				//已占用
	}
	fclose(fp);
	return 0;
}

int login(char *Account, char *Password)
{
	FILE *fp = NULL;
	fp = open_db();
	char date[170];
	fseek(fp, 0, SEEK_SET);					//偏移文件开头
	Clnode *p;
	p = client_head;

	while(feof(fp) == 0)
	{
		memset(date, 0, sizeof(date));
		fgets(date, sizeof(date), fp);
		if(!strcmp(Account, p->user_data.Account))		//判断已登陆
		{
			fclose(fp);
			return 3;
		}
		if(!strncmp(date, Account, strlen(Account)))	//寻找登陆账号
		{
			if(!strncmp(date + 20, Password, strlen(Password)))	//比对密码
			{
				fclose(fp);
				return 1;								//正确
			}
			else
			{
				fclose(fp);
				return 0;								//密码错误
			}
		}
	}
	fclose(fp);			
	return 2;				//账号错误
}

void add_userdate(char *Account, int sockfd)			//登陆成功 装填用户信息
{
	Clnode *p;
	FILE *fp = NULL;

	p = find_client(sockfd);
	fp = open_db();
	char date[170];
	char password[21];
	fseek(fp, 0, SEEK_SET);						//偏移文件开头
	while(feof(fp) == 0)
	{
		memset(date, 0, sizeof(date));
		fgets(date, sizeof(date), fp);				//读一行
		
		if(!strncmp(date, Account, strlen(Account)))	//寻找账号
		{
			sscanf(date, "%20s%20s%20s%50s%50s%3d\n", 		
				p->user_data.Account, password,  
				p->user_data.Nickname, p->user_data.Motto, 
				p->user_data.Email, &(p->user_data.Pic));
		}
	}
	fclose(fp);	
}

int send_userlist(int info, int sockfd)			//发送用户列表和信息
{
	Clnode *p;
	char date[1005], sendbuf[1024];
	int len = 0, num = 0;
	if(info != 1)	//登陆失败
		return 0;
	for(p = client_head; p != NULL ; p = p -> next)
	{							//已登陆的账号信息；数据包长度判断
		
		if(strlen(p->user_data.Account) && ((len + 148) < 1005))
		{
			num++;
			len += sprintf(date + len, "%s:%s:%s:%s:%d:", p->user_data.Account,   
					p->user_data.Nickname, p->user_data.Motto, 
					p->user_data.Email, p->user_data.Pic);
		}
		if((len + 148) > 1005)	//长数据包发送
		{
			pack_020(sendbuf, date, num);
			sock_send(sendbuf, sockfd);
			memset(date, 0, sizeof(date));
			num = 0;
			len = 0;
		}
	}
	if(len != 0)				//短数据包发送
	{
		pack_020(sendbuf, date, num);
		sock_send(sendbuf, sockfd);
		memset(date, 0, sizeof(date));
	}
	return 0;
}

void change(char *Account, char *Nickname, char *Motto, char *Email, int Pic)
{
	FILE *fp = NULL;
	fp = open_db();
	char date[170];
	char password[21];
	int i;
	for(i = 0 ; feof(fp) == 0 ; i++)
	{
		memset(date, 0, sizeof(date));
		fgets(date, sizeof(date), fp);				//读一行
		
		if(!strncmp(date, Account, strlen(Account)))	//寻找账号
		{
			sscanf(date + 20, "%20s", password);
			break;
		}
	}
	fseek(fp, i*164, SEEK_SET);				//偏移指定位置
	fprintf(fp, "%-20s%-20s%-20s%-50s%-50s%-3d\n", 
		Account, password, Nickname, Motto , Email, Pic);
	fclose(fp);
}

